package or.T2583;

import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.Deque;
import java.util.LinkedList;
import java.util.PriorityQueue;

public class T2583 {


    public long kthLargestLevelSum(TreeNode root, int k) {
        Deque<TreeNode> deque=new LinkedList();
        deque.add(root);
        long[] sums=new long[k];
        Arrays.fill(sums,-1);
        while (!deque.isEmpty()){
            long sum=0;
            int size=deque.size();
            for(int i=0;i<size;i++){
                TreeNode tmp=deque.poll();
                sum+=tmp.val;
                if (tmp.left != null) deque.offerLast(tmp.left);
                if (tmp.right != null) deque.offerLast(tmp.right);
            }
            if(sum>sums[0]){
                sums[0]=sum;
                Arrays.sort(sums);
            }
        }
        return sums[0];


    }
    @Test
    public void test() {



    }

}
